import {reqShowStaff,reqAttendance, reqRemoveStaff,reqAddStaff,reqSearchStaff} from '../api/index'
const state={
    staff:[],
    attendance:[],
}
const mutations={
    SHOWSTAFF(state,allstaff){
        state.staff=allstaff
    },
    ATTENDANCE(state,attendance){
        state.attendance=attendance
    },
    REMOVESTAFF(){
        // 删除数据后，再次发送查询请求，得到数据库最新数据
        this.dispatch('showstaff')
    },
    ADDSTAFF(){
        this.dispatch('showstaff')
    },
    SEARCHSTAFF(){

    }
}
const actions={
    // 展示全部员工信息
    async showstaff({commit}){
        let result = await reqShowStaff()
        if(result.status==0){
            commit('SHOWSTAFF',result.data)
    
        }else{
            return Promise.reject(new Error('faile'))
        }
    },
    // 展示员工考勤信息
    async attendance({commit}){
        let result = await reqAttendance()
        if(result.status==0){
            commit('ATTENDANCE',result.data)
    
        }else{
            return Promise.reject(new Error('faile'))
        }
    },
    // 删除某员工
    async removeStaff({commit},id){
        let result = await reqRemoveStaff(id)
        console.log(result);
        
        if(result.status==0){
            commit('REMOVESTAFF')
    
        }else{
            return Promise.reject(new Error('faile'))
        }
    },
    // 添加员工
    async addStaff({commit},staff){
        let result = await reqAddStaff(staff)
        if(result.status==0){
            commit('ADDSTAFF')
        }else{
            return Promise.reject(new Error('faile'))
        }
    },
    // 查找某员工
    async searchstaff({commit},id){
        let result = await reqSearchStaff(id)
        if(result.status==0){
            commit('SEARCHSTAFF')
        }else{
            return Promise.reject(new Error('faile'))
        }
    },
}
const getters={

}
export default{
    state,
    actions,
    mutations,
    getters
}